﻿using dTools;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.AxHost;

namespace Demo.TestOpen
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {

            var path = @"C:\Users\PVer\Desktop\单片";
            var to = $@"C:\Users\PVer\Desktop\TEST";
            while (true)
            {
                var allFiles = FileHelper.GetFiles(path).OrderBy(x => x.LastWriteTime).ToList();

                DateTime create = DateTime.Now;

                if (allFiles.Count == 1)
                {
                    if ((DateTime.Now - allFiles[0].LastWriteTime).TotalSeconds >= 3)
                    {
                        //Move
                        var move = FileHelper.Combine(to, $"S_{DateTime.Now:yyyyMMdd_HHmmss_fff}");
                        File.Move(allFiles[0].FullName, Path.Combine(move, allFiles[0].Name));
                    }
                }
                else
                {
                    for (int i = 0; i < allFiles.Count; i++)
                    {
                        var f = allFiles[i];
                        var nDate = DateTime.Now;
                        var fDate = f.LastWriteTime;

                        //最后一组
                        if (i == allFiles.Count - 1)
                        {
                            //当前时间
                            nDate = DateTime.Now;
                        }
                        else
                        {
                            //不是最后一组
                            var n = allFiles[i + 1];
                            nDate = n.LastWriteTime;
                        }

                        if ((nDate - fDate).TotalSeconds > 3)
                        {
                            //Move
                            allFiles = allFiles.Where(x => x.LastWriteTime < nDate).ToList();
                            if (allFiles.Count > 0)
                            {
                                var move = FileHelper.Combine(to, $"{DateTime.Now:yyyyMMdd_HHmmss_fff}");
                                allFiles.ForEach(x =>
                                {
                                    File.Copy(x.FullName, Path.Combine(move, x.Name), true);
                                    File.Delete(x.FullName);
                                    Console.WriteLine($"moved .. {x.FullName}");
                                });
                                break;
                            }
                        }
                    }
                }

                Thread.Sleep(100);
            }

            bool runone;
            System.Threading.Mutex run = new System.Threading.Mutex(true, "BaizePTGUIABC", out runone);
            if (runone)
            {
                run.ReleaseMutex();
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
            }
            else
            {
                MessageBox.Show("Test Error,Has one");
                Application.Exit();
            }
        }
    }
}
